package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.LiveListScreenAdapter;
import com.xgkj.diyiketang.data.AdvertiseInfo;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.data.SchoolInfo;
import com.xgkj.diyiketang.fragment.ZhiboFirstPageFragment;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskResultCode;
import com.xgkj.diyiketang.task.GetUserDetailTask;
import com.xgkj.diyiketang.task.LiveListTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.util.SharedUtils;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

public class ScreenActivity extends BaseActivity  {
    private PullToRefreshListView mListView;
    private LiveListScreenAdapter mAdapter;
    private int isLive = -1;
    private int mTuijianListTaskId = -1;
    private int currentPage = 1;
    private int size = 10;
    private NoDataUtil noDataUtil;
    private int mZhiboType;
    private String versionCode;
    private String subject;
    private String grade;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen);
        mListView = (PullToRefreshListView) findViewById(R.id.zhibo_list);
        View view1 = findViewById(R.id.screen);
        noDataUtil = new NoDataUtil(mContext, view1);
        getTuijianList();
    }
    private void getTuijianList() {
        Intent intent = getIntent();
        if (intent!=null){
            if (intent.getExtras()!=null){
                subject = intent.getExtras().getString("subject");
                grade = intent.getExtras().getString("grade");
            }
        }
        mZhiboType = 1;
        if (mTuijianListTaskId != -1) {
            return;
        }
        if (currentPage == 0) {
            mListView.setNoNextPage();
            return;
        }
        LiveListTask task = new LiveListTask(size, currentPage, null, null, mZhiboType, "", subject, grade,"", "", "0","1");
        task.addListener(new MyListener(mContext));
        mTuijianListTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    class MyListener extends ShowToastTaskEventListener {
        public MyListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == mTuijianListTaskId) {
                mTuijianListTaskId = -1;
//                if (mListView.getVisibility() == View.VISIBLE) {
                if (currentPage == 1) {
                    mAdapter.clearData();
                    mListView.onRefreshComplete();
                    mListView.enableLoadMore();
                    mListView.setSelection(0);
                } else {
                    mListView.onLoadDataComplete(true);
                }
                List<EatShowUserInfo> fansList = new ArrayList<EatShowUserInfo>();
                if (data != null) {
                    fansList = (List<EatShowUserInfo>) data;
                }

                if (fansList != null && fansList.size() > 0) {
                    mAdapter.updateData(fansList);
                }

                if (mAdapter != null && mAdapter.getCount() > 0) {
                    noDataUtil.ListViewNoEmpty(mListView);
                } else {
                    noDataUtil.ListViewEmpty(mListView, null, null, -1);
                }
                currentPage = result;
            }

        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
//            if (errorCode == TaskResultCode.NEED_LOGIN) {
//                ScreenActivity.this.showFirstPage();
//            }
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == mTuijianListTaskId) {
                mTuijianListTaskId = -1;
                if (currentPage == 1) {
                    mListView.onRefreshComplete();
                } else {
                    mListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    }

}
